﻿using System;
using System.Web.Mvc;
using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Web.Attributes;
using EnetaMvc.Web.Models;

namespace EnetaMvc.Web.Controllers
{
    public class BlogPostsController : BaseController
    {
        private readonly IContentService _contentService;
        private readonly INewsService _newsService;
        private readonly IUserService _userService;

        public BlogPostsController(IContentService contentService, INewsService newsService, IUserService userService)
        {
            _contentService = contentService;
            _newsService = newsService;
            _userService = userService;
        }

        [HttpGet]
        public ActionResult Index(int page = 1)
        {
            if (page < 1)
                page = 1;

            var posts = _newsService.ListPublishedBlogPosts(page, 10);

            return View(posts);
        }

        [HttpGet]
        public ActionResult Details(long id)
        {
            BlogPostDto blogPost;

            if (User.IsInRole("Administrator"))
            {
                blogPost = _newsService.GetBlogPostById(id);
            }
            else
            {
                blogPost = _newsService.GetPublishedBlogPostById(id);
            }

            if (blogPost == null)
                return HttpNotFound("Cannot find blog post with ID " + id);

            var model = new BlogPostDetailsModel();
            model.Post = blogPost;
            model.Blog = _newsService.GetBlogById(blogPost.BlogId);

            return View(model);
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult ContentAction(long id, string actionName, long[] tags, string comment = "")
        {
            var article = _newsService.GetBlogPostById(id);
            article.SaveTagIds = tags;

            if (actionName == Resources.EnetaMvc.Approve)
            {
                var user = _userService.GetUserByToken(User.Identity.Name);

                article.PublishStateId = 1;
                article.PublishStateById = user.Id;
                article.PublishStateDate = DateTime.Now;
                _contentService.SaveContentItemSlagLog(article.Id, article.PublishStateId, article.PublishStateById);
            }
            if (actionName == Resources.EnetaMvc.Reject)
            {
                var user = _userService.GetUserByToken(User.Identity.Name);

                article.PublishStateId = 3;
                article.PublishStateById = user.Id;
                article.PublishStateDate = DateTime.Now;
                article.PublishComment = comment;
                _contentService.SaveContentItemSlagLog(article.Id, article.PublishStateId, article.PublishStateById);
            }
            _newsService.SaveBlogPost(article);
            return RedirectToAction("Details", new { id = id });
        }

        [HttpPost]
        [EnetaAuthorize(Roles = "User, Administrator")]
        public ActionResult AddComment(long id, string body)
        {
            var user = _userService.GetUserByToken(User.Identity.Name);

            var comment = new CommentDto();
            comment.ContentItemId = id;
            comment.Created = DateTime.Now;
            comment.AuthorId = user.Id;
            comment.Body = body;

            _contentService.SaveComment(comment);

            return RedirectToAction("Details", new { id = id });
        }
    }
}
